package io.github.maojian.dynamicprogramming;

import org.junit.Test;

/**
 * @Auther: jian.mao
 * @Date: 2022-06-21 14:43
 * @Description: https://leetcode.cn/problems/fibonacci-number/
 *
 * 0 1 1 2 3 5 8 13 21 34 55
 */
public class Q509 {
    public int fib(int n) {
        if (n < 2) {
            return n;
        } else {
            return fib(n - 1) + fib(n - 2);
        }
    }

    public int fib01(int n) {
        if (n < 2) return n;
        int first, second = 0, three = 1;
        for (int i = 2; i <= n; i++) {
            first = second + three;
            second = three;
            three = first;
        }
        return three;
    }


    @Test
    public void test() {
        System.out.println(fib(10));
        System.out.println(fib01(10));
    }
}
